#include "base.h"
#include <iostream>
using namespace std;

//selftset+++++++++++++++++++++++++++++++++++++++
int base::selftest(){
    resourceLayer* bs1 = new resourceLayer();
    resourceLayer* bs2 = new resourceLayer();

    int res[] = {10, 10, 10, 10};
    int ind[] = {10, 10, 10, 10};
    float rats[] = {.25, .25, .25, .25};
    int costs[RES_MAX][RES_MAX];
    for(int i = 0; i < RES_MAX; i++){
        for(int j = 0; j < RES_MAX; j++){
            costs[i][j] = 5;
        }
    }

    bs1->initPop(100, 1000, .5);
    bs2->initPop(100, 1000, .4);

    bs1->initInd(ind, rats, costs);
    bs2->initInd(ind, rats, costs);

    

    for(int i = 0; i < 100; i++){
    cout << "turn " << i << endl;
    cout << bs1->population() << " __ " << bs2->population() << endl;
    cout << bs1->resource(metal) << " __ " << bs2->resource(metal) << endl;
    cout << bs1->resource(chem) << " __ " << bs2->resource(chem) << endl;
    cout << bs1->resource(fuel) << " __ " << bs2->resource(fuel) << endl;
    cout << bs1->resource(crew) << " __ " << bs2->resource(crew) << endl;
    cout << bs1->update() << "___" << bs2->update() << endl;
    }
}
//===============================================
//base+++++++++++++++++++++++++++++++++++++++++++
// the base class of a planet.  handles resource
// managment.  It seperates resource into internal
// and external piles, 
//-----------------------------------------------
//constructors----------
base::resourceLayer::resourceLayer(){
    pop = -1;
    popmax = -1;
    industryPoints[0] = -1;
    industryRatios[0] = -1;
    for(int i = 0; i < RES_MAX; i++){
        resourcePoints[i] = 0;
        AVRatio[i] = 1/(RES_MAX - 1);
    }
}
base::resourceLayer::~resourceLayer(){
}
void base::resourceLayer::initPop(float seedpop, int pmax, float grate){
    if(pop == -1){
        pop = seedpop;
        popmax = pmax;
        popGRate = grate;
    }
}
void base::resourceLayer::initInd(int indseed[], float indratios[], int costs[][RES_MAX]){
    for(int i = 0; i < RES_MAX; i++){
        industryPoints[i] = indseed[i];
        industryRatios[i] = indratios[i];
        for(int j = 0; j < RES_MAX; j++){
            industryCost[i][j]= costs[i][j];
        }
    }
}
//eot-------------------
bool base::resourceLayer::update(){
    updatePop();
    updateInd();
    updateRes();
    return true;
}
bool base::resourceLayer::updatePop(){
    int growth = 0;
    if(pop <= popmax)
        growth = pop * (popGRate * ((float)(popmax - pop)/popmax));
    pop += growth;
    return true;
}
bool base::resourceLayer::updateRes(){
    for(int i = 0; i < RES_MAX; i++){
        resourcePoints[i] += industryPoints[i];
    }
}
bool base::resourceLayer::updateInd(){
    //find the free number of ind points
    int free = pop - industry();
    cout << "free---: " << free << endl;
    //find the needed resources
    int need[RES_MAX] = {0};
    for(int i = 0; i < RES_MAX; i++){
        for(int j = 0; j < RES_MAX; j++){
           need[j] += (industryCost[i][j] * industryRatios[i] * free) - (resourcePoints[j] - need[j]);
        }
    }
    for(int i = 0; i < RES_MAX; i++){
        industryPoints[i] += industryRatios[i]*free;
    }
    return true;/*
    //request what we dont have
    requestRes(need);
    //do the update & spend res
    int spend;
    for(int i = 0; i < RES_MAX; i++){

        //spend = removeRes(i, industryCost[i] * industryRatios[i] * free);
        if(spend < industryCost[i] * industryRatios[i] * free){

        }
        industryPoints += 0;

    }
    return true;*/
}
//stats-----------------
int base::resourceLayer::population(){
    return (int)pop;
}
int base::resourceLayer::resource(resourceType tp){
    return resourcePoints[tp];
}
int base::resourceLayer::availableRes(resourceType tp){
	return resourcePoints[tp] * AVRatio[tp];
}
int base::resourceLayer::industry(){
    int ret = 0;
    for(int i = 0; i < RES_MAX; i++){
        ret += industryPoints[i];
    }
    return ret;
}
int base::resourceLayer::industry(resourceType tp){
	return industryPoints[tp];
}
//resourceing-----------
int base::resourceLayer::removeAvailRes(resourceType tp, int num){
}
int base::resourceLayer::removeRes(resourceType tp, int num){
    if(num > resourcePoints[tp]){
        int ret = resourcePoints[tp];
        resourcePoints[tp] = 0;
        return ret;
    }
    resourcePoints[tp] -= num;
    return num;
}
int base::resourceLayer::addRes(resourceType tp, int num){
    resourcePoints[tp] += num;
    return resourcePoints[tp];
}
void base::resourceLayer::requestRes(int* resArr){
    
}
//===============================================
